import { definePage, reactive, ref, toRefs, onShow } from '../../vue-mini/index'
import { getWeatherTipsText } from '../../utils/plugus'
import {
  getNowWeatherApi,
  getLifeIndexApi,
  getWeatherAllApi,
  getRealtimeBatchApi,
  getaWeekWeatherApi,
  getWeatherPm25Api,
} from '../../utils/request'

definePage({
  setup(options) {
    onShow(() => {
      const cityNm  = options.cityNm || wx.getStorageSync('cityNm')
      if( cityNm ) { //本地有记录直接查询保存的城市
        wx.setStorageSync('cityNm', cityNm)
        state.cityNm = cityNm
        initWeather({ cityNm })
        return
      }
      // 本地没有数据或没有选择城市 进行定位授权获取用户信息
      wx.getLocation({
        success(res) {
          // 经纬度存到本地
          wx.setStorageSync('ll', {
            wgs84ll: res.longitude + ',' + res.latitude,
          })
          let params =  { wgs84ll: res.longitude + ',' + res.latitude}
          initWeather(params)
        },
      })
    })
    const state = reactive({
      count: 100,
      NowWeatherData: {}, //当前天气信息
      lifeIndexList: [], //生活指数列表
      lifeIndexData: [], //当前生活指数
      aWeekList: [], //本周天气
      pm25Data: {}, //pm2.5
      dayDataAll: {},
      weatherTipsText: '', //天气提示文案
      weatherTipsIcon: '', //天气提示图标
      addresData: {}, //地址信息
      id: '',
      cityNm: '',
    })
    const initWeather = (params)=>{
      getNowWeather(params) //当前天气
      getLifeIndex(params) //生活指数
      getWeatherAll(params) //所有的天气
      //getRealtimeBatch() //专业数据 付费
      getaWeekWeather(params) //一周天气
      getWeatherPm25(params) //pm25
    }
    // 获取当前的天气
    const getNowWeather = (ll) => {
      getNowWeatherApi(ll).then((res) => {
        const { result } = res.data
        state.NowWeatherData = result
        // 获取提示信息
        const { x, i } = getWeatherTipsText(result.weather)
        state.weatherTipsText = x //提示文案
        state.weatherTipsIcon = i //提示图标
      })
    }
    // 获取一周的天气
    const getaWeekWeather = (ll) => {
      getaWeekWeatherApi(ll).then((res) => {
        const { result } = res.data
        // 添加提示信息属性
        state.aWeekList = result.map((e) => {
          return { ...e, weatherTips: getWeatherTipsText(e.weather) }
        })
      })
    }
    // 获取生活指数
    const getLifeIndex = (ll) => {
      getLifeIndexApi(ll).then((res) => {
        const { result } = res.data
        state.lifeIndexList = result
        state.lifeIndexData = result[0]
        console.log(result)
      })
    }
    // 获取pm25
    const getWeatherPm25 = (ll) => {
      getWeatherPm25Api(ll).then((res) => {
        const { result } = res.data
        state.pm25Data = result
      })
    }
    // 判断是不是阴天  阴天更改封面
    const overcast = ref(false)
    // 日出时间 日落时间取整
    const sunriseTime = ref(null) //日出时间
    const sunsetTime = ref(null) //日出时间
    const nowTime = ref(new Date().getHours()) //当前时间
    // 获取所有天气情况
    const getWeatherAll = (ll) => {
      getWeatherAllApi(ll).then((res) => {
        const {
          data: {
            result: { futureDay, futureHour, realTime, today },
          },
        } = res
        state.dayDataAll.realTime = realTime
        state.dayDataAll.today = today
        overcast.value = realTime.wtNm.includes('阴')
        sunriseTime.value = today.wtSunr.split(':')[0] * 1
        sunsetTime.value = today.wtSuns.split(':')[0] * 1
      })
    }

    // 专业数据
    const getRealtimeBatch = (ll) => {
      getRealtimeBatchApi(ll).then((res) => {
        const { dtList } = res.data.result
      })
    }

    return {
      ...toRefs(state),
      getWeatherTipsText,
      sunriseTime,
      sunsetTime,
      nowTime,
      overcast,
    }
  },
})
